package com.mxpio.erp.sales.controller;

import com.mxpio.erp.common.jpa.model.OrderLineKey;
import com.mxpio.erp.common.sales.entity.RejectBillLine;
import com.mxpio.erp.common.sales.service.RejectBillLineService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Api(value = "RejectBillLineController", tags = { "退货通知单行接口" })
@RestController
@RequestMapping("/erp/sales/rbline/")
public class RejectBillLineController {

    @Autowired
    private RejectBillLineService service;

    @GetMapping("list")
    @ApiOperation(value = "退货通知单行列表", notes = "获取退货通知单行列表（无分页）", httpMethod = "GET")
    public Result<List<RejectBillLine>> list(@ApiParam(name="查询参数") String criteria) throws UnsupportedEncodingException {
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        List<RejectBillLine> rejectBillLines = service.list(RejectBillLine.class, c);
        return Result.OK(rejectBillLines);
    }

    @GetMapping("page")
    @ApiOperation(value = "退货通知单行列表", notes = "获取退货通知单行列表（分页）", httpMethod = "GET")
    public Result<Page<RejectBillLine>> page(@ApiParam(name="查询参数") String criteria, @ApiParam(name="每页条数")Integer pageSize, @ApiParam(name="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        Page<RejectBillLine> rejectBillLines = service.listPage(RejectBillLine.class, page, c);
        return Result.OK(rejectBillLines);
    }

    @GetMapping("list/{bizNo}/{lineNo}")
    @ApiOperation(value = "根据ID获取退货通知单行", notes = "根据ID获取退货通知单行", httpMethod = "GET")
    public Result<RejectBillLine> getById(@ApiParam(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo, @ApiParam(name="行号") @PathVariable(name = "lineNo", required = true)String lineNo) {
        OrderLineKey lineKey = new OrderLineKey();
        lineKey.setLineNo(lineNo);
        lineKey.setBizNo(bizNo);
        RejectBillLine rejectBillLine = service.getById(RejectBillLine.class, lineKey);
        return Result.OK(rejectBillLine);
    }

    @PostMapping("add")
    @ApiOperation(value = "新增退货通知单行", notes = "新增退货通知单行", httpMethod = "POST")
    public Result<RejectBillLine> add(@RequestBody RejectBillLine rejectBillLine) {
        service.save(rejectBillLine);
        return Result.OK(rejectBillLine);
    }

    @PutMapping("edit")
    @ApiOperation(value = "编辑退货通知单行", notes = "编辑退货通知单行", httpMethod = "PUT")
    public Result<RejectBillLine> edit(@RequestBody RejectBillLine rejectBillLine) {
        service.update(rejectBillLine);
        return Result.OK(rejectBillLine);
    }

    @DeleteMapping("remove/{ids}")
    @ApiOperation(value = "根据id删除退货通知单行", notes = "删除退货通知单行,多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后", httpMethod = "DELETE")
    public Result<RejectBillLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            String[] pk = key.split(":");
            OrderLineKey lineKey = new OrderLineKey();
            lineKey.setBizNo(pk[0]);
            lineKey.setLineNo(pk[1]);
            service.remove(RejectBillLine.class, lineKey);
        }
        return Result.OK();
    }
}
